﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Windows;
using System.Windows.Browser;
using PmuRegistry.Silverlight.PmuRegistryDataServiceProxy;
using Microsoft.Maps.MapControl;
using System.Collections.Generic;
using PmuRegistry.Silverlight.Utilities;

namespace PmuRegistry.Silverlight
{
	public partial class App : Application
	{
		public bool LoggedIn { get; set; }
		public string UserName { get; set; }
		//public Dictionary<int, string> StatusList { get; set; }
		public ApplicationIdCredentialsProvider credentials { get; set; }
		public App()
		{
			this.Startup += this.Application_Startup;
			this.Exit += this.Application_Exit;
			this.UnhandledException += this.Application_UnhandledException;			
			InitializeComponent();
		}

		private void Application_Startup(object sender, StartupEventArgs e)
		{
			if ((e.InitParams != null) && (e.InitParams.ContainsKey("BaseServiceUrl")))
				Resources.Add("BaseServiceUrl", e.InitParams["BaseServiceUrl"]);						
			this.LoggedIn = false;
			ApplicationIdCredentialsProvider credentialsProvider = new ApplicationIdCredentialsProvider();
			if (HtmlPage.Document.DocumentUri.Host.ToLower().Contains("naspi.tva.com"))
				credentialsProvider.ApplicationId = "Aufnog7bZCZle0B4NI8taKiDfOejE8xKJzpjRDrb-rTMXHfH21NFgNpO6ABZpBYB";
			else if (HtmlPage.Document.DocumentUri.Host.ToLower().Contains("naspipmuregistry"))
				credentialsProvider.ApplicationId = "AkolredUwaoQxqGYxW82LixLQ6ekTMiPTCVRbWfF90fOwNEdFyQXbloh2UrdVZQc";				
			else if (HtmlPage.Document.DocumentUri.Host.ToLower().Contains("chadesoweb"))
				credentialsProvider.ApplicationId = "Aqe4CdYLIoO8eqoXJAUoxcMDmaKaL37JAvgRTHZKaxsjEAabSM4OWgK34ClvC9Qh";
			else if (HtmlPage.Document.DocumentUri.Host.ToLower().Contains("chaaesoweb"))
				credentialsProvider.ApplicationId = "AqRPc-TZWsUE726g0in_jWzgvte_nx4VZMcWXVhUo-0KjdrHnSAxWY3csg0hQn3j";
			else	//it is localhost
				credentialsProvider.ApplicationId = "AqBBWYs9xERY4hGsCXIp0FKpZrphipQW4LVmdwwXaJ9MfWnO-jX92-jGPKXzulQ7";

			this.credentials = credentialsProvider;
			this.RootVisual = new MasterLayoutControl();

            //Set default system settings if no settings exist.
            Common.SetDefaultSystemSettings(false);
		}

		private void Application_Exit(object sender, EventArgs e)
		{
		
		}
		private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
		{
			// If the app is running outside of the debugger then report the exception using
			// the browser's exception mechanism. On IE this will display it a yellow alert 
			// icon in the status bar and Firefox will display a script error.
			if (!System.Diagnostics.Debugger.IsAttached)
			{

				// NOTE: This will allow the application to continue running after an exception has been thrown
				// but not handled. 
				// For production applications this error handling should be replaced with something that will 
				// report the error to the website and stop the application.
				e.Handled = true;
				Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });
			}
		}
		private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
		{
			try
			{
				string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
				errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

				System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
			}
			catch (Exception)
			{
			}
		}
				
	}
}
